<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
 * TODO
 * datetime not working
*/
 
 
 
// no direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.filesystem.path' );
jimport( 'joomla.application.component.view');
jimport( 'joomla.database.database' );





 
// Create a new query object.
//$query = $db->getQuery(true);
 
// Insert columns.
//$columns = array('user_id', 'profile_key', 'profile_value', 'ordering');
 
// Insert values.
//$values = array(1001, $db->quote('custom.message'), $db->quote('Inserting a record using insert()'), 1);
 
// Prepare the insert query.
//$query
//    ->insert($db->quoteName('#__user_profiles'))
//    ->columns($db->quoteName($columns))
//    ->values(implode(',', $values));
 
// Reset the query using our newly populated query object.
//$db->setQuery($query);


/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */
 

class wotclanViewupload extends JView
{
    function display($tpl = null)
    {
    	if (JFactory::getUser()->authorise('core.add', 'com_wotclan')) 
      {
      	include JPATH_COMPONENT.DS.'functions.php';
        $db =& JFactory::getDbo();
        $greeting = "Uploader votre replay! (V0.0.1)";
        $this->assignRef( 'greeting', $greeting );
        
        // get user info
        
    		$user =& JFactory::getUser();
				$usr_id = $user->get('id');
				

				
         //Retrieve file details from uploaded file, sent from upload form
				$file = JRequest::getVar('file_upload', null, 'files', 'array');
 
				//Import filesystem libraries. Perhaps not necessary, but does not hurt
				jimport('joomla.filesystem.file');
 
				//Clean up filename to get rid of strange characters like spaces etc
				$filename = JFile::makeSafe($file['name']);
        parent::display($tpl);
        if ($_FILES["file"]["error"] > 0)
  			{
  				echo "Error: " . $_FILES["file"]["error"] . "<br>";
  			//}
				//else
  			//{
  				//echo "Upload: " . $_FILES["file_upload"]["name"] . "<br>";
  				//echo "Type: " . $_FILES["file_upload"]["type"] . "<br>";
  				//echo "Size: " . ($_FILES["file_upload"]["size"] / 1024) . " kB<br>";
  				//echo "Stored in: " ."media/wotclan/replays/" . $_FILES["file"]["name"]  ;
 
  			}

 
				//Set up the source and destination of the file
				// $src = $file['tmp_name'];
				$src = $_FILES["file_upload"]['tmp_name'];
				$dest = JPATH_ROOT.DS. "media" . DS . "wotclan" . DS . "replays" . DS . $_FILES["file_upload"]["name"];
 				//echo "<br>new dest : " . $dest ;

 
				//First check if the file has the right extension, we need jpg only
				if ( strtolower(JFile::getExt($filename) ) == 'wotreplay') {
   			if ( JFile::upload($src, $dest) ) {

    
   	 			//Redirect to a page of your choice
    		//	echo "all is ok";
          $fp = fopen($dest, 'rb');
    			while ( ($line = fgets($fp)) !== false) {
    				if ((preg_match("/mapName/i", $line)) || (preg_match("/frags/i", $line))){
    					analyse_team($line,$dest);
//    					echo "<h1>test:".$team1['name'][0]."</h1>";
    			//		$keywords = preg_split("/,/", $line);
//    	
  									}
            }
   			} else {
      		//Redirect and throw an error message
      		echo "error message";
   			}
		 } else {
   		//Redirect and notify user file is not right extension
   		echo "no right extension";
		 }
    } else {
    		$greeting = "Vous n'avez pas acces a cette section!";
        $this->assignRef( 'greeting', $greeting );
 
        parent::display($tpl);
    		
    }
	}
}




